package com.gomyb.utils;


import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019/4/15 12:59
 */
@Slf4j
public class UUIDUtil {

    private UUIDUtil() {
    }

    /**
     * 订单号生成器
     */
    public static class OrderNum {

        private static AtomicInteger count = new AtomicInteger(1);

        private static Long oldTime = System.currentTimeMillis();

        public static synchronized String getOrderNum() {
            long currentTime = System.currentTimeMillis();
            if ((oldTime + 1000) < currentTime) {
                oldTime = currentTime;
                count.set(1);
            } else {
                count.addAndGet(1);
            }
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            return format.format(currentTime) + String.format("%03d", count.get());
        }

        public static void main(String[] args) {
            for (int i = 0; i < 1000000; i++) {
                System.out.println(getOrderNum());
                /*try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
        }
    }

    /**
     * 邀请码生成器
     */
    public static class InviteCode {
        private InviteCode() {
        }

        // start in 2019 as 'a'
        private final static Integer ascCount = 77;

        // 'a'
        private final static Integer start = 97;

        // 'z'
        private final static Integer end = 122;

        private final static Long ONE_HOUR = 3600000L;

        //    private static Long oldTime = 0L;
        private static Long oldTime = 0L;

        private static Integer count = 0;


        public static synchronized String getUUID(String datacenter) {
            try {
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                long time = calendar.getTime().getTime();
                if (oldTime == 0L) {
                    count = 1;
                    oldTime = time;
                } else {
                    if ((time - ONE_HOUR) >= oldTime) {
                        oldTime = time;
                        count = 1;
                    } else {
                        count++;
                    }
                }
                int year = calendar.get(Calendar.YEAR);     // letter
                int month = calendar.get(Calendar.MONTH) + 1;   // letter
                int day = calendar.get(Calendar.DAY_OF_MONTH);  // number
                int hour = calendar.get(Calendar.HOUR_OF_DAY);  // letter
                // 长度
                String[] chas = new String[7];

                chas[0] = String.valueOf((char) (year % 26 + start));
                chas[1] = String.valueOf((char) (month % 12 + start));
                chas[2] = day + "";
                chas[3] = String.valueOf((char) (hour % 24 + start));
                chas[4] = count + "";

                int k = year % 26 + start;  //余
                int y = year / 26 - ascCount;    //count
                String s = String.valueOf((char) k);
                if (y > 0) {
                    s = getAfter(new StringBuilder(s), y).toString();
                }
                chas[5] = s;
                chas[6] = datacenter;
                if (count == 1) {
                    System.out.println("aaaaa");
                }
                return toString(chas);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("uuid create error ... ", e);
            }
            throw new RuntimeException("uuid create failed ... ");
        }

        private static StringBuilder getAfter(StringBuilder s, int y) {
            s.append(((char) (y % 26 + start)));
            if ((y / 26) > 0) getAfter(s, (y / 26 - 1));
            return s;
        }

        private static String toString(String[] chas) {
            StringBuilder sb = new StringBuilder();
            for (String cha : chas) {
                sb.append(cha);
            }
            return sb.toString();
        }


        public static void main(String[] args) {
            while (true) {
                System.out.println(getUUID("t"));
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
        // 04-15 12:34:56
        // a-a-15 a :15:c
        // a-a-15 a nb
    }

}
